package cn.lijian.goods.admin.order.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.servlet.BaseServlet;
import cn.lijian.goods.order.domain.Order;
import cn.lijian.goods.order.service.OrderService;
import cn.lijian.goods.pager.PageBean;
import cn.lijian.goods.user.domain.User;

public class AdminOrderServlet extends BaseServlet {

	private OrderService orderService=new OrderService();
	
	/**
	 * 获取当前页的页码
	 * @param request
	 * @return
	 * @author lijian
	 */
	private int getPageCurrent(HttpServletRequest request){
		int pc=1;
		String param=request.getParameter("pc");
		if(param!=null&&!param.trim().isEmpty()){
			try {
				pc=Integer.parseInt(param);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return pc;
	}
	
	/**
	 * 
	 * 截取 URL 页面中的分页导航中需要使用它作为超链接的目标
	 * @param request
	 * @return
	 * @author lijian
	 */
	private String getUrl(HttpServletRequest request){
		//假设请求是   http://localhost:8080/goods/jsps/xx?sssss=s&sss=s
		String uri=request.getRequestURI();//   /goods/jsps/xx
		String str=request.getQueryString();//   xx?sssss=s&sss=s
		uri=uri+"?"+str;
		//如果 URL 中存在 pc 参数，截取掉，如果没有就行
		
		int index=uri.lastIndexOf("&pc");
		if(index!=-1){
			uri=uri.substring(0,index);
		}
		return uri;
	}

	/**
	 * 查询所有订单
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//得到 pageCurrent，如果页面传递，获取，如果没有，设置 1
		int pc=getPageCurrent(request); 
		//得到 URL 
		String url=getUrl(request);
	
		//使用 service 查询,得到 pagebean
		PageBean<Order>pageBean=orderService.findAll(pc);
		//给pagebean 设置 URL 
		pageBean.setUrl(url);
		request.setAttribute("pb", pageBean);
		return "f:/adminjsps/admin/order/list.jsp";
	}
	
	
	
	/**
	 * 按订单状态查询
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String findByStatus(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//得到 pageCurrent，如果页面传递，获取，如果没有，设置 1
		int pc=getPageCurrent(request); 
		//得到 URL 
		String url=getUrl(request);
		int status=Integer.parseInt(request.getParameter("status"));
		//使用 service 查询,得到 pagebean
		PageBean<Order>pageBean=orderService.findByStatus(status, pc);
		//给pagebean 设置 URL 
		pageBean.setUrl(url);
		request.setAttribute("pb", pageBean);
		return "f:/adminjsps/admin/order/list.jsp";
	}
	
	
	/**
	 * 查看订单详细信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String load(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String oid=request.getParameter("oid");
		Order order=orderService.load(oid);
		request.setAttribute("order", order);
		String btn=request.getParameter("btn");//说明了用户点击 哪个超链接来访问本方法的
		request.setAttribute("btn", btn);
		return "f:/adminjsps/admin/order/desc.jsp";
		
	}
	
	
	
	/**
	 * 取消订单
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String cancle(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String oid=request.getParameter("oid");
		int status=orderService.findStatus(oid);
		if(status!=1){
			request.setAttribute("code", "error");
			request.setAttribute("msg","订单状态不对，不能取消");
			return "f:/jsps/msg.jsp";
		}else{
			orderService.updateStatus(oid, 5);//设置状态为取消
			request.setAttribute("code", "success");
			request.setAttribute("msg", "订单已取消");
			return "f:/adminjsps/admin/msg.jsp";
		}
	}
	
	/**
	 * 发货
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String deliver(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String oid=request.getParameter("oid");
		int status=orderService.findStatus(oid);
		if(status!=2){
			request.setAttribute("code", "error");
			request.setAttribute("msg","订单状态不对，不能发货");
			return "f:/jsps/msg.jsp";
		}else{
			orderService.updateStatus(oid, 3);//设置状态为已发货
			request.setAttribute("code", "success");
			request.setAttribute("msg", "订单已发货，请查看物流信息");
			return "f:/adminjsps/admin/msg.jsp";
		}
	}
	

}
